const Model = require("./Model")
class adminUser extends Model {
  //查找
  static findAll(keyword, currentPage, pageSize) {
    return new Promise((resolve, reject) => {
      const sql = `SELECT id,title,content,DATE_FORMAT(crea_time, '%Y-%m-%d %H:%i:%s') AS crea_time FROM notice
      WHERE title LIKE ?
      ORDER BY crea_time DESC
      LIMIT ? OFFSET ?;`
      this.query(sql, [`%${keyword}%`, pageSize, currentPage])
        .then(resolve)
        .catch(reject)
    })
  }
  //获取总数
  static count(keyword) {
    return new Promise((resolve, reject) => {
      const sql = "SELECT COUNT(*) AS count FROM `notice` where title LIKE ?"
      this.query(sql, [`%${keyword}%`])
        .then((result) => {
          const count = result[0].count
          resolve(count)
        })
        .catch(reject)
    })
  }
  //保存公告
  static saveNotice(title, content) {
    return new Promise((resolve, reject) => {
      const sql = "INSERT INTO notice (title, content) VALUES (?,?)"
      this.query(sql, [title, content]).then(resolve).catch(reject)
    })
  }

  //更新公告
  static updateNotice(id, title, content) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE notice SET title = ?, content=? WHERE id = ?"
      this.query(sql, [title, content, id]).then(resolve).catch(reject)
    })
  }
  //查询token
  static verifyToken(token) {
    return new Promise((resolve, reject) => {
      const sql = "SELECT id,salt FROM `user` where token = ?"
      this.query(sql, [token]).then(resolve).catch(reject)
    })
  }
}

module.exports = adminUser
